#include "dialogitem.h"
#include "buttonitem.h"

DialogItem::DialogItem(const QString &imgName,
                       const QPoint &leftTop,
                       const QPoint &rightBottom,
                       QGraphicsScene *scene,
                       QGraphicsItem *parent) :
    ImageItem(imgName, leftTop, rightBottom, scene, parent), onDrag(false)
{
    this->setZValue(1);
    this->setVisible(false);
    closeButton = new ButtonItem(":/dialog-close", QPoint(325, 0), QPoint(400, 75), scene, this);
//    closeButton = new ButtonItem(":/dialog-close", QPoint(224, 149), QPoint(272, 197), scene, this);
    closeButton->setZValue(2);
    connect(closeButton, SIGNAL(clicked(ButtonItem*)), this, SLOT(buttonClicked(ButtonItem*)));
}

void DialogItem::buttonClicked(ButtonItem *who)
{
    if (who == closeButton)
    {
        this->setVisible(false);
//        closeButton->setVisible(false);
        emit dialogClosed();
    }
}

void DialogItem::popup()
{
    this->setVisible(true);
//    closeButton->setVisible(true);
}

void DialogItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (!onDrag)
    {
        onDrag = true;
        toCenter = event->pos();/*-pos();*/
    }
}

void DialogItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
//    qDebug() << pos();
    setPos(event->pos()-toCenter+pos());
//    toCenter = event->pos()-pos();
    parentItem()->update();
}

void DialogItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (onDrag)
        onDrag = false;
}
